package com.example.itrain.demo.controller;

import com.example.itrain.demo.model.User;
import com.example.itrain.demo.response.PageQuery;
import com.example.itrain.demo.response.Result;
import com.example.itrain.demo.service.UserService;
import com.example.itrain.demo.auth.BCryptUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author rain
 * @since 2024-11-18
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("list")
    public Result list(PageQuery pageQuery) {
        List list = userService.list(pageQuery.Page());
        return Result.success(list);
    }

    @PostMapping("register")
    public Result register(@RequestBody User user) {
        String encryptedPassword = BCryptUtils.encrypt(user.getAccountPassword());
        user.setAccountPassword(encryptedPassword);
        return userService.save(user) ? Result.success("保存成功") : Result.error("保存失败");
    }
}
